package Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.jfree.chart.JFreeChart;

import Utils.GetHelper;
import Utils.JFreeChartHelper;
import Utils.TimeHelper;

import com.opensymphony.xwork2.ActionSupport;

import Beans.BoxDetails;
import Beans.Branch;
import Beans.Config;
import Beans.Terminal;
import DAO.BoxDao;
import DAO.BranchDao;
import DAO.ConfigDao;
import DAO.TerminalDao;

@SuppressWarnings("serial")
public class TerminalAction extends ActionSupport {

	private Terminal terminal = new Terminal();
	private Config config = null;
	private BoxDetails boxDetails = null;

	private String returnType;
	Map<String, Object> message;

	private int terminalID;// 终端id
	private int branch;
	private String oldpassword;// 旧密码
	private String newpassword;// 新密码
	private String serverIP;// 服务器IP
	private String portNum;// 服务器端口

	TerminalDao terminalDao = new TerminalDao();
	BranchDao branchDao = new BranchDao();
	BoxDao boxDao = new BoxDao();
	ConfigDao configDao = new ConfigDao();
	
	List<Terminal> terminals;
	List<Branch> branches;
	List<Config> configs = configDao.getAllconfig();
	
	private JFreeChart chart; 

	/**
	 * 获取所有的网点
	 * 
	 * @return
	 */
	public String getAllBranch() {
		branches = branchDao.getAllbranch();
		return "success";
	}

	/**
	 * 获取终端信息
	 * 
	 * @return
	 */
	public String getTerminalById() {
		terminal = terminalDao.getTerminalById(terminalID);
		System.out.println(terminal.getBranch().getBranchId()
				+ "--------------------------------------->");
		getAllBranch();
		return "success";
	}

	/**
	 * 获取终端详情
	 * 
	 * @return
	 */
	public String getTerminalDetails() {
		terminal = terminalDao.getTerminalById(terminalID);
		boxDetails = new BoxDetails(boxDao.boxStatusDetail(terminalID, -1, -1),
				boxDao.boxStatusDetail(terminalID, -1, 1),
				boxDao.boxStatusDetail(terminalID, -1, 0),
				boxDao.boxStatusDetail(terminalID, -1, 5),
				boxDao.boxStatusDetail(terminalID, 1, -1),
				boxDao.boxStatusDetail(terminalID, 1, 1),
				boxDao.boxStatusDetail(terminalID, 2, -1),
				boxDao.boxStatusDetail(terminalID, 2, 1),
				boxDao.boxStatusDetail(terminalID, 3, -1),
				boxDao.boxStatusDetail(terminalID, 3, 1),
				boxDao.boxStatusDetail(terminalID, 4, -1),
				boxDao.boxStatusDetail(terminalID, 4, 1),
				boxDao.boxStatusDetail(terminalID, -1, 2));
		return "details";
	}

	/**
	 * 返回反向SSH页面
	 * 
	 * @return
	 */
	public String commitSSH() {
		terminal = terminalDao.getTerminalById(terminalID);
		return "commitSSH";
	}

	/**
	 * 提交反向SSH
	 */
	public void upSSH() {
		config = configs.get(0);
		String str = "controller?funcID=0021&status=6&terminalId=" + terminalID
				+ "&serverIP=" + serverIP + "&portNum=" + portNum;
		GetHelper.doHttpGet(config.getValue() + str);
	}

	/**
	 * 编辑终端
	 * 
	 * @return
	 */
	public String editTerminal() {
		Terminal t = terminalDao.getTerminalById(terminalID);
		terminal.setBranch(branchDao.getBranchById(branch));
		terminal.setTerminalId(terminalID);
		terminal.setHardVer(t.getHardVer());
		terminal.setSoftVer(t.getSoftVer());
		terminal.setStatus(t.getStatus());
		terminal.setLoginTime(t.getLoginTime());
		terminalDao.edit(terminal);
		config = configs.get(0);
		GetHelper
		.doHttpGet(config.getValue()+"controller?funcID=0030&terminalId="
				+ terminalID);
		String str = "controller?funcID=0029&terminalId="+ terminalID;
		GetHelper
				.doHttpGet(config.getValue()+str);
		return "success";
	}

	/**
	 * 删除终端
	 * 
	 * @return
	 */
	public String deleteTerminal() {
		if(boxDao.returnBoxesInThisTerminal(terminalID)==0){
			terminal = terminalDao.getTerminalById(terminalID);
			terminal.setStatus(2);
			terminalDao.edit(terminal);
		}
		return "success";
	}

	/**
	 * 查看终端密码
	 * 
	 * @return
	 * @throws IOException
	 */
	public String getTermainalPwd() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		terminal = terminalDao.getTerminalById(terminalID);
		out.print(terminal.getTerminalPwd());
		out.close();
		return null;
	}

	/**
	 * 跳转至修改终端密码
	 * 
	 * @return
	 */
	public String linkUpdateTerminalPwd() {
		terminal = terminalDao.getTerminalById(terminalID);
		return "updateTerminalPwd";
	}

	/**
	 * 修改密码
	 * 
	 * @return
	 * @throws IOException
	 */
	public String updateNewPassword() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		try {
			terminal = terminalDao.getTerminalById(terminalID);
			terminal.setTerminalPwd(newpassword);
			terminalDao.edit(terminal);
			config = configs.get(0);
			GetHelper
					.doHttpGet(config.getValue()+"controller?funcID=0030&terminalId="
							+ terminalID);
			GetHelper
					.doHttpGet(config.getValue()+"controller?funcID=0029&terminalId="
							+ terminalID);
			out.print("0-" + newpassword);
		} catch (Exception e) {
			out.print("1-" + newpassword);
		} finally {
			out.close();
		}
		return null;
	}

	/**
	 * 验证旧密码
	 * 
	 * @return
	 * @throws IOException
	 */
	public String checkPassword() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		if (this.oldpassword.equals(terminalDao.getTerminalById(terminalID)
				.getTerminalPwd() == null ? "" : terminalDao.getTerminalById(
				terminalID).getTerminalPwd())) {
			out.print("0");
		} else {
			out.print("1");
		}
		out.close();
		return null;
	}

	/**
	 * 新增终端
	 * 
	 * @return
	 */
	public String registerTerminal() {
		terminal.setBranch(branchDao.getBranchById(branch));
		terminalDao.save(terminal);
		return "success";
	}

	public String returnAllTerminals() {
		terminals = terminalDao.returnAllTermials();
		for(Terminal t:terminals){
			if(TimeHelper.isTerminalOff(t.getLoginTime())){
				t.setStatus(1);
			}
		}
		// System.out.println(terminals.get(0).getBranch().getBranchId());
		return "success";
	}
	
	public String getPieChart() throws IOException{
		chart = JFreeChartHelper.createChart();
		return "pieChart";
	}

	public String edit() {
		System.out.println("terminalID ------->" + terminalID);
		return "success";
	}

	public int getTerminalID() {
		return terminalID;
	}

	public void setTerminalID(int terminalID) {
		this.terminalID = terminalID;
	}

	public List<Terminal> getTerminals() {
		return terminals;
	}

	public void setTerminals(List<Terminal> terminals) {
		this.terminals = terminals;
	}

	public String getReturnType() {
		return returnType;
	}

	public void setReturnType(String returnType) {
		this.returnType = returnType;
	}

	public Map<String, Object> getMessage() {
		return message;
	}

	public void setMessage(Map<String, Object> message) {
		this.message = message;
	}

	public Terminal getTerminal() {
		return terminal;
	}

	public void setTerminal(Terminal terminal) {
		this.terminal = terminal;
	}

	public List<Branch> getBranches() {
		return branches;
	}

	public void setBranches(List<Branch> branches) {
		this.branches = branches;
	}

	public int getBranch() {
		return branch;
	}

	public void setBranch(int branch) {
		this.branch = branch;
	}

	public String getOldpassword() {
		return oldpassword;
	}

	public void setOldpassword(String oldpassword) {
		this.oldpassword = oldpassword;
	}

	public String getNewpassword() {
		return newpassword;
	}

	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}

	public Config getConfig() {
		return config;
	}

	public void setConfig(Config config) {
		this.config = config;
	}

	public List<Config> getConfigs() {
		return configs;
	}

	public void setConfigs(List<Config> configs) {
		this.configs = configs;
	}

	public String getServerIP() {
		return serverIP;
	}

	public void setServerIP(String serverIP) {
		this.serverIP = serverIP;
	}

	public String getPortNum() {
		return portNum;
	}

	public void setPortNum(String portNum) {
		this.portNum = portNum;
	}

	public BoxDetails getBoxDetails() {
		return boxDetails;
	}

	public void setBoxDetails(BoxDetails boxDetails) {
		this.boxDetails = boxDetails;
	}

	public JFreeChart getChart() {
		return chart;
	}

	public void setChart(JFreeChart chart) {
		this.chart = chart;
	}
	
}
